/**
 * 
 * purpose: tab container...
 * @auth: Ning
 * date: 2009-9-17
 * 
 * */
package com.components.tabcontainer
{
	import com.components.button.ButtonEvents;
	import com.components.button.ButtonGroup;
	import com.components.button.MCButton;
	import com.components.scrollpane.ScrollPane;
	
	import core.ease.EaseFade;
	import core.ease.EaseFadeEvents;
	import core.section.Controller;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;

	public class TabContainer extends Controller
	{
		//START...
		
		//PROPERTY...
		private var naviClass:Class;
		
		private var scrollPane:ScrollPane;
		
		private var naviItem:Sprite;
		
		private var naviItem_1:Sprite;
		
		private var tar:Sprite;
		
		private var naviContainer:Sprite;
		
		private var pane:Sprite;
		
		private var startX:Number;
		
		private var startY:Number;
		
		private var vNavi:Boolean;
		
		private var space:Number;
		
		private var buttonGroup:ButtonGroup;
		
		private var _currentSection:EaseFade;
		
		private var prevSection:EaseFade;
		
		private var isReadyFadeOut:Boolean;
		
		private var isReadyFadeIn:Boolean;
				
		private var sections:Array;
		
		//CONSTRUCTOR...
		public function TabContainer(target:DisplayObject)
		{
			
			tar = target as Sprite;
			
			super(target);
			
			init();
			
		}
		
		//METHODS...
		
		//initiailizing...
		private function init():void
		{
			
			for(var i:uint = 0;i < tar.numChildren;i++)
			{
				
				try{
										
					this[tar.getChildAt(i).name] = tar.getChildAt(i);
										
				}catch(err:Error){
					
					trace("Instance Miss: " + tar.getChildAt(i).name);
					
				}
				
			}
			
			naviClass = getDefinitionByName(getQualifiedClassName(naviItem)) as Class;
			
			vNavi = naviItem.x == naviItem_1.x;
			
			startY = naviItem.y - naviContainer.y;
			
			startX = naviItem.x - naviContainer.x;
			
			space = vNavi ? naviItem.y + naviItem.height - naviItem_1.y : naviItem.x + naviItem.width - naviItem_1.x;
			
			tar.removeChild(naviItem);
			
			tar.removeChild(naviItem_1);
			
			buttonGroup = new ButtonGroup;
			
			sections = new Array;
			
			scrollPane = new ScrollPane(pane);
			
			configEventListeners();
			
		}
		
		//config event listenrs...
		private function configEventListeners():void
		{
			
			buttonGroup.addEventListener(ButtonEvents.BUTTON_SELECTED,btnGroupSelectedHandler);
			
		}
		
		//config section event...
		private function configSectionListener(section:EaseFade):void
		{
			
			section.addEventListener( EaseFadeEvents.READY_FOR_EASE_IN,onSectionReadyEaseIn);
			
			section.addEventListener( EaseFadeEvents.READY_FOR_EASE_OUT,onSectionReadyEaseOut);
			
			section.addEventListener( EaseFadeEvents.EASE_IN_OVER,onSectionEaseInOver);
			
			section.addEventListener( EaseFadeEvents.EASE_OUT_OVER,onSectionEaseOutOver);
			
			section.addEventListener( EaseFadeEvents.READY_FOR_DISPLAY,onReadyForDisplay);
						
		}
		
			
		
		//handlers...
		
		//on section ready for ease in...
		private function onSectionReadyEaseIn(evt:EaseFadeEvents):void
		{
			
			isReadyFadeIn = true;
			
			if(isReadyFadeOut)
			{
				
				prevSection.easeOut();
				
			}
			
			if(!prevSection)
			{
				
				onSectionEaseOutOver(null);
				
			}
			
			
		}
				
		//on section ready for ease out...
		private function onSectionReadyEaseOut(evt:EaseFadeEvents):void
		{
			
			isReadyFadeOut = true;
			
			if(isReadyFadeIn)
			{
				
				prevSection.easeOut();
				
			}
			
		}
		
		
		//on section ease in over...
		private function onSectionEaseInOver(evt:EaseFadeEvents):void
		{
						
			_currentSection.phase = EaseFade.DISPLAY;	
			
		}
		
		
		
		//on section ease out over...
		private function onSectionEaseOutOver(evt:EaseFadeEvents):void
		{
						
			scrollPane.content = _currentSection.target;
						
			_currentSection.easeIn();			
			
		}
		
		//on section ready for display...
		private function onReadyForDisplay(evt:EaseFadeEvents):void
		{
			
			_currentSection.display();
			
			dispatchEvent(new TabContainerEvents(TabContainerEvents.SECTION_DISPLAY,currentSection,prevSection));			
			
		}
		
		
		
		//button group selected handler...
		private function btnGroupSelectedHandler(evt:ButtonEvents):void
		{
			//navi...
			var button:MCButton = evt.selectedItem;
			
			naviContainer.swapChildren(button.target.parent,naviContainer.getChildAt(naviContainer.numChildren - 1));
			
			//section...
			
			if(currentSection is EaseFade)
			{
				
				prevSection = _currentSection;
				
				_currentSection = button.data as EaseFade;
				
				isReadyFadeIn = false;
				
				isReadyFadeOut = false;
				
				prevSection.phase = EaseFade.EASE_OUT;
				
				currentSection.phase = EaseFade.EASE_IN;
				
				
			}else{
				
				
				_currentSection = button.data as EaseFade;
				
				currentSection.phase = EaseFade.EASE_IN;
				
			}
			
			dispatchEvent(new TabContainerEvents(TabContainerEvents.SECTION_CHANGE,currentSection,prevSection));
			
		}
		
		//add section...
		public function addSection(section:EaseFade,strLabel:String):void
		{
									
			var item:Sprite = new naviClass;
			
			var label:TextField = (item as Sprite).getChildByName("label") as TextField;
			
			label.text = strLabel;
			
			label.autoSize = TextFieldAutoSize.LEFT;
			
			label.mouseEnabled = false;
			
			var button:Sprite = (item as Sprite).getChildByName("button") as Sprite;
			
			button.width = Math.max(label.x + label.width,button.width + label.x);
			
			item.x = startX;
			
			item.y = startY;
			
			var bgButton:MCButton = new MCButton(button as MovieClip);
			
			bgButton.data = section;
			
			configSectionListener(section);
			
			buttonGroup.addButton(bgButton);
						
			naviContainer.addChild(item);
			
			if(vNavi)
			{
				
				startY = item.y + item.height - space;				
				
			}else{
				
				startX = item.x + item.width - space; 
				
			}
			
		}
		
		//get current section...
		public function get currentSection():EaseFade
		{
			
			return _currentSection;
			
		}
		
		//END
	}
}